package com.longtime.aliyun.ocr.service.impl;

import com.aliyun.tea.TeaException;
import com.aliyun.teaopenapi.Client;
import com.aliyun.teaopenapi.models.OpenApiRequest;
import com.aliyun.teaopenapi.models.Params;
import com.aliyun.teautil.models.RuntimeOptions;
import com.longtime.aliyun.ocr.constants.AliyunOcrReqConstants;
import com.longtime.aliyun.ocr.service.IOcrService;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @author xhc
 * @version 1
 * @date 2025-05-26 13:01:16
 * @description 阿里云OCR服务类
 */
public class AliyunOcrService implements IOcrService {

    @Resource
    private Client client;

    @Resource
    private Params params;

    @Override
    public Map<String, ?> parse(String imageUrl) {
        try {
            Map<String, Object> body = new HashMap<>();
            body.put(AliyunOcrReqConstants.BODY_KEY_IMAGE_URL, imageUrl);
            RuntimeOptions runtime = new RuntimeOptions();
            OpenApiRequest request = new OpenApiRequest()
                    .setBody(body);
            return client.callApi(params, request, runtime);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

}
